//
//  JJJNetworkTool.swift
//  myBlog
//
//  Created by 姜军君 on 15/11/28.
//  Copyright © 2015年 JJJ. All rights reserved.
//

import UIKit
import AFNetworking

/// 网络错误domain
private let JJJNetErrorDomain = "com.jjjConpany.error.network"

class JJJNetworkTool: AFHTTPSessionManager {
    
    // MARK: - 网络参数
    let clientId = "1405261283"
    let clientSecret = "3ce0d9536f2cc7e36fde3471e38c98cc"
    let redirectUri = "http://www.baidu.com"
    
    /// 网络单例
    static let sharedTool:JJJNetworkTool = {
        
        let tool = JJJNetworkTool(baseURL: NSURL(string: "https://api.weibo.com/"), sessionConfiguration: nil)
        tool.responseSerializer.acceptableContentTypes = NSSet(objects:"application/json", "text/json", "text/javascript", "text/plain") as Set<NSObject>
        
        return tool
        }()
    
    /// 加载用户信息
    func loadUserInfo(uid: String, finished: JJJNetFinishCallBack) {
    
        let baseURL = "2/users/show.json"
        let params:[String: AnyObject] = [
            "access_token":JJJUserAccount.loadUserAccount()!.access_token!,
            "uid":uid
        ]
        
        requestGET(baseURL, params: params, finished: finished)
    }
    
    /// 获取授权URL
    func oauthURL() -> NSURL {
        
        let urlString = "https://api.weibo.com/oauth2/authorize?client_id=\(clientId)&redirect_uri=\(redirectUri)"
        
       return NSURL(string: urlString)!
    }
    
    // MARK: - 加载token
    func loadToken(code:String, finished:JJJNetFinishCallBack) {
    
        let urlString = "https://api.weibo.com/oauth2/access_token"
        let params = [
            "client_id":clientId,
            "client_secret":clientSecret,
            "grant_type":"authorization_code",
            "redirect_uri":redirectUri,
            "code":code
        ]
        
//       responseSerializer = AFHTTPResponseSerializer()
        POST(urlString, parameters: params, success: { (_,JSON) -> Void in
        
//            print(NSString(data: JSON as! NSData, encoding: NSUTF8StringEncoding))
            finished(result: JSON as? [String:AnyObject], error: nil)
            }) { (_, error) -> Void in
                
                print(error)
                finished(result: nil, error: error)
        }
    }
    
    typealias JJJNetFinishCallBack = (result:[String:AnyObject]?,error:NSError?) -> ()
    /// 封装AFN的GET方法
    func requestGET(urlString: String, params: [String: AnyObject], finished: JJJNetFinishCallBack) {
    
        GET(urlString, parameters: params, success: { (_, JSON) -> Void in
            if let result = JSON as? [String: AnyObject] {
                
                finished(result: result, error: nil)
            }else {
                
                print("空数据")
                let error = NSError(domain: JJJNetErrorDomain, code: -1, userInfo: ["netError":"空数据"])
                finished(result: nil, error: error)
            }
            }) { (_, error) -> Void in
                
                print(error)
                finished(result: nil, error: error)
        }
    }
}
